<?php

// +----------------------------------------------------------------------
// | 热门活动信息
// +----------------------------------------------------------------------
// | Author: Helly
// +----------------------------------------------------------------------
// | Date: 2015/01/05 19:12
// +----------------------------------------------------------------------

namespace Admins\Controller;

use Common\Controller\AdminBase;
use Common\Model\Model;


class ToppicController extends AdminBase {

    protected $doAction='';

    //初始化模型
    protected function _initialize() {
        parent::_initialize();
        $this->doAction = I('request.doAction', $_POST['doAction'], 'trim');
    }

    //随手拍专题
    public function suipai(){

        //显示
        if($this->doAction=='index'){
            if (IS_POST) {
                $this->redirect('suipai?doAction=index', $_POST);
            }
            //查询条件
            $where = '';
            //搜索
            $search = I('get.search');
            if (!empty($search)) {
                $this->assign("search", $search);
                //添加开始时间
                $start_time = I('get.start_time');
                if (!empty($start_time)) {
                    $start_time = strtotime($start_time);
                    $where.=' and S.create_time>'.$start_time;
                    $this->assign('start_time', $start_time);
                }
                //添加结束时间
                $end_time = I('get.end_time');
                if (!empty($end_time)) {
                    $end_time = strtotime($end_time);
                    $where.=' and S.create_time<'.$end_time;
                    $this->assign('end_time', $end_time);
                }
                /*if ($end_time > 0 && $start_time > 0) {
                    $where.=' and S.create_time>'.$start_time.' and S.create_time<'.$end_time;
                }*/
                //搜索关键字
                $keyword = \Input::getVar(I('get.keyword'));
                if (!empty($keyword)) {
                    $this->assign("keyword", $keyword);
                    $where.=' and S.title like "%'.$keyword.'"';
                }
                //状态
                $status = I('get.status');
                $this->assign('status',$status);
                if(isset($_GET['status'])){
                    $where.=' and S.status='.$status;
                }

                //获奖
                $ifwin = I('get.ifwin');
                $this->assign('ifwin',$ifwin);
                if(isset($_GET['ifwin']) && $ifwin<3){
                    $where.=' and TS.ifwin='.$ifwin;
                }

            }else{
                $where.=' and S.status=true';
                $this->assign('ifwin',3);
            }

            $Model=new Model();
            $arr=$Model
                ->table(array(
                        C('DB_PREFIX').'toppic_suipai'=>'TS',
                        C('DB_PREFIX').'suipai'=>'S',
                        C('DB_PREFIX').'user'=>'U',
                    )
                )
                ->where('TS.userid=U.userid and S.id=TS.workid'.$where)
                ->field('TS.id')
                ->select();

            $count=count($arr);

            $page = $this->Pages($count, 30);
            $list=$Model
                ->table(array(
                        C('DB_PREFIX').'toppic_suipai'=>'TS',
                        C('DB_PREFIX').'suipai'=>'S',
                        C('DB_PREFIX').'user'=>'U',
                    )
                )
                ->where('TS.userid=U.userid and S.id=TS.workid'.$where)
                ->field('TS.ifwin,S.title,S.msgnum,S.clicknum,S.id,TS.create_time,S.image,U.uavatar,U.openuid,TS.userid,S.status')
                ->order('TS.id DESC')
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
            $this->assign('Page', $page->show())
                ->assign('count', $count)
                ->assign('data', $list);

            $this->display('Toppic:Suipai:index');
        }


        //批量处理信息
        if($this->doAction=='suipaibatch'){
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            $doAct=I('get.do');
            switch($doAct){
                case 'win':
                    $data=array('ifwin'=>true);
                    $msg='获奖';
                    break;
                case 'nowin':
                    $data=array('ifwin'=>false);
                    $msg='取消获奖';
                    break;
            }
            foreach ($ids as $id) {
                M('ToppicSuipai')->where(array('workid' => $id))->save($data);
            }

            $this->success($msg.'成功！');

        }



    }




}
